﻿using _51_AOP.Utility.Filter;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace _51_AOP.Controllers
{
    [CustomResourceFilter]
    public class HomeController : Controller
    {

        public HomeController(ILogger<HomeController> logger)
        {
            Console.WriteLine(this.GetType().FullName+"开始被构造");
        }
        #region ResourceFilter
        [CustomResourceFilter]
        public IActionResult Index()
        {
            {
                // 业务逻辑
                Console.WriteLine("执行页面逻辑代码");
                ViewBag.Date = DateTime.Now.ToString("yyyy-MM-dd ss");
            }
            return View();
        }
        [CustomAsyncResourceFilter]
        public IActionResult Privacy()
        {
            {
                // 业务逻辑
                Console.WriteLine("执行页面逻辑代码");
                ViewBag.Date = DateTime.Now.ToString("yyyy-MM-dd ss");
            }
            return View();
        }
        #endregion

        #region ActionFilter
        // [CustomActionFilter] 这个如果放日志程序会爆错
        [TypeFilter(typeof(CustomActionFilterAttribute))]
        public IActionResult index2(int id,int id1)
        {
            {
                Console.WriteLine($"执行index2逻辑代码");
            }
            ViewBag.user = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                id = id,
                name = "aggie",
                age=34
            });
            ViewData["userinfo"] = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                id = id1,
                name = "aggie--ViewData",
                age = 35
            });
            object msg = "欢迎你";
            return View(msg);
        }
        [TypeFilter(typeof(CustomAsyncActionFilterAttribute))]
        public IActionResult index3(int id, int id1)
        {
            {
                Console.WriteLine($"执行index3逻辑代码");
            }
            ViewBag.user = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                id = id,
                name = "aggie",
                age = 34
            });
            ViewData["userinfo"] = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                id = id1,
                name = "aggie--ViewData",
                age = 35
            });
            object msg = "欢迎你";
            return View(msg);
        }
        #endregion


        #region ResultFilter
        [CustomResultFilter]
        public IActionResult index4(int id)
        {
            // 这里是要渲染视图的
            {
                Console.WriteLine("index4到代码执行中");

                
            }
            return View();
            
        }
        [CustomResultFilter]
        public IActionResult index5(int id)
        {
            // 这里不用创建视图 直接返回数据。就像接口一样操作
            return Json(new
            {
                Id = 123,
                Name = "aggie",
                Age = 18
            });
        }
        [CustomAsyncResultFilterAttribute]
        public IActionResult index6()
        {
            // 这里不用创建视图 直接返回数据。就像接口一样操作
            return Json(new
            {
                Id = 111,
                Name = "aggie",
                Age = 18
            });
        }
        #endregion
        [CustomResourceFilter]
        [CustomAlwaysRunResultFilter]
        public IActionResult index7()
        {
            // 这里不用创建视图 直接返回数据。就像接口一样操作
            Console.WriteLine("执行代码。。。。");
            return Json(new
            {
                Id = 111,
                Name = "aggie",
                Age = 18
            });
        }
        // 其他action都支持 control的特性，这个action不支持
        [CustomAllowAnonymous]
        public ActionResult index8()
        {
            Console.WriteLine("执行代码。。。。");
            return Json(new
            {
                Id = 222,
                Name = "aggie",
                Age = 18
            });
        }

        [TypeFilter(typeof(CustomExceptionFilterAttribute))]
        public ActionResult index9() {
            throw new Exception("这里错误了");
        }


        [TypeFilter(typeof(CustomExceptionFilterAttribute))]
        public ActionResult Error()
        {
            throw new Exception("这里错误了");
        }


    }
}